package com.ctyon.watchmanager.ui.fragment;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ctyon.common.event.AddWatch;
import com.ctyon.common.util.DataUtil;
import com.ctyon.common.util.TSnackbarUtil;
import com.ctyon.netlibrary.net.revert.BaseResponseEntity;
import com.ctyon.netlibrary.use.BaseObserver;
import com.ctyon.watchmanager.BasicApp;
import com.ctyon.watchmanager.R;
import com.ctyon.watchmanager.bean.AddBean;
import com.ctyon.watchmanager.databinding.FragmentAddWatchInputBinding;
import com.ctyon.watchmanager.model.net.service.NetService;
import com.ctyon.watchmanager.present.AddInputPresenter;
import com.ctyon.watchmanager.ui.BaseFragment;
import com.orhanobut.logger.Logger;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * CreateDate：19-6-25 on 上午9:14
 * Describe:
 * Coder: lee
 */
public class AddWatchInputFragment extends BaseFragment {

    public static final String TAG = "AddWatchInputFragment";
    public static final String MEID = "meid";

    private FragmentAddWatchInputBinding mBinding;
    private AddBean addBean;
    private int length = 0;

    public static AddWatchInputFragment newInstance(String meid) {
        final AddWatchInputFragment fragment = new AddWatchInputFragment();
        final Bundle args = new Bundle();
        args.putString(MEID, meid);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_add_watch_input, container, false);

        return mBinding.getRoot();

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String meid = getArguments().getString(MEID);
        Logger.t(LTAG).d("meid = " + meid);
        addBean = new AddBean();
        if (!DataUtil.isEmptyAnd(meid)) {
            length = meid.length();
            Logger.t(LTAG).d("length = " + meid.length());
            if (length < 30) {
                addBean.setMeid(meid);
            }
        }
        mBinding.setAddBean(addBean);
        mBinding.setInputPresenter(addInputPresenter);
        mBinding.editMeid.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (null != s)
                mBinding.editMeid.setSelection(s.toString().length());
            }
        });
    }

    private final AddInputPresenter addInputPresenter = () -> {
        String meid = addBean.getMeid();
        String nick = addBean.getNick();
        if (DataUtil.isEmptyOr(meid) || meid.length() < 4) {
            TSnackbarUtil.TSBError(getActivity(), "设备号有误");
            return;
        }
        meid = meid.substring(4, meid.length());
        NetService.getInstance()
                .addWatch(BasicApp.getInstance().getManager().getManagerId(), meid, nick)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponseEntity>() {
                    @Override
                    public void onSuccess(BaseResponseEntity responseEntity) {
                        Logger.d("onSuccess : " + responseEntity.toString());
                        EventBus.getDefault().post(new AddWatch());
                        getActivity().finish();
                    }
                });
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        Logger.t(LTAG).d("onDestroy");
    }
}
